export default function wsjConfirm(opt) {
  /**
   * ===一个参数,对象形式===
   * @param {string} opt.content 必填 弹窗内容,html字符串
   * @param {function} opt.ok 选填 点击确定按钮的回调
   * @param {function} opt.cancel 选填 点击取消按钮的回调
   * @param {string} opt.okText 选填 确定按钮文字
   * @param {string} opt.cancelText 选填 取消按钮文字
   * ===两个参数===
   * @param {string} content 必填 弹窗内容
   * @param {function} ok 选填 点击确定按钮的回调
   * @example
   * wsjConfirm({content:'哈哈哈'})
   * wsjConfirm('嘎嘎',()=>{})
   */
  var cfg = {
    content: '...',
    ok: null,
    cancel: null,
    okText: '确定',
    cancelText: '取消'
  }
  //下方懒得做防呆判断
  if (arguments.length === 1) {
    if (typeof arguments[0] === 'string') {
      cfg.content = arguments[0]
    } else {
      cfg = { ...cfg, ...opt }
    }
  } else if (arguments.length === 2) {
    cfg.content = arguments[0]
    cfg.ok = arguments[1]
  } else {
    throw new Error('wsjConfirm参数数量错误')
  }

  var oDiv = document.createElement('div')
  var str = `<div class="wsj-confirm-wrapper-mask">
  <div class="wsj-confirm-wrapper-body">
   ${cfg.content}
    <div class="wsj-confirm-btn-wrap">
      <button type='button' class="wsj-confirm-btn-common wsj-confirm-btn-common-a" data-wsjconfirm="cancel">${cfg.cancelText}</button>
      <button type='button' class="wsj-confirm-btn-common wsj-confirm-btn-common-b" data-wsjconfirm='ok'>${cfg.okText}</button>
    </div>
  </div>
</div>`
  oDiv.innerHTML = str
  document.body.appendChild(oDiv)
  oDiv.addEventListener('click', handleOdivClick)
  function handleOdivClick(e) {
    if (e.target.dataset.wsjconfirm === 'cancel') {
      if (cfg.cancel === null) { shutDown() }
      if (typeof cfg.cancel === 'function') { cfg.cancel(shutDown) }
    }
    if (e.target.dataset.wsjconfirm === 'ok') {
      if (cfg.ok === null) { shutDown() }
      if (typeof cfg.ok === 'function') { cfg.ok(shutDown) }
    }
  }
  function shutDown() {
    oDiv.removeEventListener('click', handleOdivClick)
    document.body.removeChild(oDiv)
  }
}
